#!/usr/bin/perl
use strict;
package nicks;
use modules::encode;

sub new {
    my ($dbh,$remote,$nsnum,$cdebug,$input) = @_;
    my $sth;
    my $num;
    my $nick;
    my $host;
    my $isoper = 0;
#0  1     2    3     4         5                6                           7      8      9
#AD N tonberry 1 1013328883 tonberry mcns150.docsis51.scvmaxonline.com.sg DKnDOW ADAAm :kooja

#0  1   2     3     4      5     6            7     8      9      10
#AF N Client1 1 947957573 User userhost.net +oiwg DAqAoB AFAAA :Generic Client.

#  0   1     2       3
#ADAAm N tonberry 1013328904

#num char(5), nick varchar(9), host varchar(30), auth varchar (9), oper int(1)
    if ($$input[4] eq "") {
	
	$sth = $dbh->prepare("UPDATE ninfo SET nick = '$$input[2]' WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
    }
    else {

	if ($$input[7] =~ /\+/) {
	    $num = $$input[9]; $nick = $$input[2]; $host = "$$input[5]\@$$input[6]";
	  
	}
	if ($$input[7] =~ /\+o/) {
	    $isoper = 1;
	}
	if ($$input[7] !~ /\+/) {
	    $num = $$input[8]; $nick = $$input[2]; $host = "$$input[5]\@$$input[6]";
	}
	$sth = $dbh->prepare("SELECT name FROM server WHERE num = '$$input[0]' AND numcode = '".encode::encode($$input[0])."';");
	$sth->execute;
	my $server = $sth->fetchrow_array;
	$sth = $dbh->prepare("insert into ninfo values ('$num','$nick','$host',0,'$isoper','$server', '".encode::encode($num)."');");
	$sth->execute;
	$sth=$dbh->prepare("SELECT rnick FROM nicks WHERE rnick = '$nick';");
	$sth->execute;
	if (my $true = $sth->fetchrow_array) {
	    print $remote "$nsnum O $num :The nickname that you are using is registered.\n";
	    print $remote "$nsnum O $num :Please change nick or identify with me.\n";
	}
    }


}
1;
